package crud;

import javax.persistence.EntityManager;

import entities.Employee;
import entities.User;

public class EmployeeCRUD extends AbstractGRUD<Employee> {
	public EmployeeCRUD() {
		super(Employee.class);
	}

	// Tao Employee va User cung luc
	public String create_Employee_User(String address, String emgContact, String firstName,
			String lastName, String idUser, String password, String role) {
		String result = "success";
		EntityManager em = this.getEm();
		try {
			User user = new User(idUser, password, role);
			Employee employee = new Employee(address, emgContact, firstName,
					user, lastName);
			
			em.getTransaction().begin();
			em.persist(user);
			em.persist(employee);
			em.getTransaction().commit();
		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();
		}
		return result;
	}
	
	public String create_Employee_User(User user, Employee emp ) {
		String result = "success";
		EntityManager em = this.getEm();
		try {

			
			em.getTransaction().begin();
			em.persist(user);
			em.persist(emp);
			em.getTransaction().commit();
		} catch (Exception e) {
			result = e.getMessage();
			em.getTransaction().rollback();
		}
		return result;

	}
}
